package com.mdc.mansion.api.web.modules.system.web.login;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.mdc.mansion.api.web.common.interceptor.NoLoginCheck;
import com.mdc.mansion.api.web.common.utils.login.LoginTicket;
import com.mdc.mansion.api.web.common.utils.login.LoginUtil;
import com.mdc.mansion.api.web.common.web.BaseController;
import com.mdc.mansion.api.web.modules.system.bo.LoginUser;
import com.mdc.mansion.common.response.Result;
import com.mdc.mansion.common.response.ResultCode;
import com.mdc.mansion.core.modules.restaurant.AccountService;

/**<p>com.mdc.mansion.api.web.modules.system.web.login.LoginController</p>
 * <p>Title: LoginController</p>
 * <p>Description: TODO</p>
 * <p>Copyright: Copyright © 2010-2014</p>
 * <p>Company: jumei</p>
 * @author xiang.liu
 * @version 1.0
 */
@RequestMapping("${adminPath}")
@Controller
public class LoginController extends BaseController {

    @Resource
    private AccountService accountService;
    @ModelAttribute("loginUser")
    public LoginUser getUser(@RequestParam String data) {
       LoginUser loginUser= JSONObject.parseObject(data,LoginUser.class);
       return loginUser;
    }

    @NoLoginCheck
    @RequestMapping("/login")  
    public Result login(@ModelAttribute("loginUser")LoginUser loginUser){
        Result restultResult = new Result(ResultCode.COMMON_SUCCESS);
        String loginAccount=loginUser.getLoginAccount();
        String password = loginUser.getPassword();
        accountService.login(loginAccount, password);
        // 登录认证完毕 开始计算ticket信息
        LoginTicket loginTicket = LoginUtil.createUserLoginTicket(loginAccount);
        LoginUtil.putLoginTicketInCache(loginTicket);
        String token = loginTicket.getToken();
        restultResult.setProperty("token", token);
        return restultResult;
    }
}
